package com.boc.ewr.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.business.entity.AcctClassAFdbk;
import com.boc.ewr.business.entity.AcctDtlClassA1;
import com.boc.ewr.business.entity.AcctDtlClassA2;
import com.boc.ewr.business.entity.AcctDtlClassA3;
import com.boc.ewr.business.mapper.AcctClassAFdbkMapper;
import com.boc.ewr.business.mapper.AcctDtlClassA1Mapper;
import com.boc.ewr.business.mapper.AcctDtlClassA2Mapper;
import com.boc.ewr.business.mapper.AcctDtlClassA3Mapper;
import com.boc.ewr.business.service.IAcctClassAFdbkService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.events.Event;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 《上门对账反馈清单》-A类账户
 * @Author: jeecg-boot
 * @Date: 2023-05-26
 * @Version: V1.0
 */
@Service
public class AcctClassAFdbkServiceImpl extends ServiceImpl<AcctClassAFdbkMapper, AcctClassAFdbk> implements IAcctClassAFdbkService {

    @Autowired
    private AcctDtlClassA1Mapper acctDtlClassA1Mapper;

    @Autowired
    private AcctDtlClassA2Mapper acctDtlClassA2Mapper;

    @Autowired
    private AcctDtlClassA3Mapper acctDtlClassA3Mapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createData(String caMon, String caPeriodNo, Date startTime, Date endTime) {
        // 先删除所有之前数据
        this.remove(null);
        // 查询A1到A3所有数据，汇总到一个List
        List<AcctClassAFdbk> acctClassAFdbkList = new ArrayList<>();

        // 生成wrapper
        LambdaQueryWrapper<AcctDtlClassA1> a1Wrapper = new LambdaQueryWrapper();
        if (!"".equals(caMon)) {
            a1Wrapper.eq(AcctDtlClassA1::getCaMon, caMon);
        }
        if (!"".equals(caPeriodNo)) {
            a1Wrapper.eq(AcctDtlClassA1::getCaPeriodNo, caPeriodNo);
        }
        if (startTime != null && endTime != null) {
            a1Wrapper.between(AcctDtlClassA1::getCreateTime, startTime, endTime);
        } else if (startTime == null && endTime != null) {
            a1Wrapper.le(AcctDtlClassA1::getCreateTime, endTime);
        } else if (startTime != null && endTime == null) {
            a1Wrapper.ge(AcctDtlClassA1::getCreateTime, startTime);
        }

        LambdaQueryWrapper<AcctDtlClassA2> a2Wrapper = new LambdaQueryWrapper();
        if (!"".equals(caMon)) {
            a2Wrapper.eq(AcctDtlClassA2::getCaMon, caMon);
        }
        if (!"".equals(caPeriodNo)) {
            a2Wrapper.eq(AcctDtlClassA2::getCaPeriodNo, caPeriodNo);
        }
        if (startTime != null && endTime != null) {
            a2Wrapper.between(AcctDtlClassA2::getCreateTime, startTime, endTime);
        } else if (startTime == null && endTime != null) {
            a2Wrapper.le(AcctDtlClassA2::getCreateTime, endTime);
        } else if (startTime != null && endTime == null) {
            a2Wrapper.ge(AcctDtlClassA2::getCreateTime, startTime);
        }

        LambdaQueryWrapper<AcctDtlClassA3> a3Wrapper = new LambdaQueryWrapper();
        if (!"".equals(caMon)) {
            a3Wrapper.eq(AcctDtlClassA3::getCaMon, caMon);
        }
        if (!"".equals(caPeriodNo)) {
            a3Wrapper.eq(AcctDtlClassA3::getCaPeriodNo, caPeriodNo);
        }
        if (startTime != null && endTime != null) {
            a3Wrapper.between(AcctDtlClassA3::getCreateTime, startTime, endTime);
        } else if (startTime == null && endTime != null) {
            a3Wrapper.le(AcctDtlClassA3::getCreateTime, endTime);
        } else if (startTime != null && endTime == null) {
            a3Wrapper.ge(AcctDtlClassA3::getCreateTime, startTime);
        }


        // A1转化
        List<AcctClassAFdbk> acctClassAFdbkA1 = acctDtlClassA1Mapper
                .selectList(a1Wrapper)
                .stream()
                .map(acctDtlClassA1 -> {
                    AcctClassAFdbk acctClassAFdbk = new AcctClassAFdbk();
                    try {
                        // 复制属性值
                        BeanUtils.copyProperties(acctDtlClassA1, acctClassAFdbk);
                    } catch (Exception e) {
                        // 异常处理
                        e.printStackTrace();
                    }
                    // 将 Id 属性设置为空
                    acctClassAFdbk.setId(null);
                    return acctClassAFdbk;
                })
                .collect(Collectors.toList());

        // A2转化
        List<AcctClassAFdbk> acctClassAFdbkA2 = acctDtlClassA2Mapper
                .selectList(a2Wrapper)
                .stream()
                .map(acctDtlClassA2 -> {
                    AcctClassAFdbk acctClassAFdbk = new AcctClassAFdbk();
                    try {
                        // 复制属性值
                        BeanUtils.copyProperties(acctDtlClassA2, acctClassAFdbk);
                    } catch (Exception e) {
                        // 异常处理
                        e.printStackTrace();
                    }
                    // 将 Id 属性设置为空
                    acctClassAFdbk.setId(null);
                    return acctClassAFdbk;
                })
                .collect(Collectors.toList());

        // A3转化
        List<AcctClassAFdbk> acctClassAFdbkA3 = acctDtlClassA3Mapper
                .selectList(a3Wrapper)
                .stream()
                .map(acctDtlClassA3 -> {
                    AcctClassAFdbk acctClassAFdbk = new AcctClassAFdbk();
                    try {
                        // 复制属性值
                        BeanUtils.copyProperties(acctDtlClassA3, acctClassAFdbk);
                    } catch (Exception e) {
                        // 异常处理
                        e.printStackTrace();
                    }
                    // 将 Id 属性设置为空
                    acctClassAFdbk.setId(null);
                    return acctClassAFdbk;
                })
                .collect(Collectors.toList());

        // 汇总到同一个List
        acctClassAFdbkList.addAll(acctClassAFdbkA1);
        acctClassAFdbkList.addAll(acctClassAFdbkA2);
        acctClassAFdbkList.addAll(acctClassAFdbkA3);

        this.saveBatch(acctClassAFdbkList);
    }
}
